from flask import request, Blueprint, json, send_file
from services import service_event as service
import os

event_blue = Blueprint('event', __name__)
basedir = os.path.abspath(os.path.dirname(__file__))


# 管理员
@event_blue.route('/event', methods=['get'])
def getAllEvent():
    data = service.getAll()
    return data


@event_blue.route('/event/<id>', methods=['get'])
def getEventById(id):
    data = service.getById(id)
    return data


@event_blue.route('/event', methods=['put'])
def updateEvent():
    rq_data = request.get_json()
    data = service.update(rq_data)
    return data


@event_blue.route('/event', methods=['post'])
def addEvent():
    print(request.form)
    dicts = request.form.to_dict()
    print(dict)
    file = request.files['attach']
    print(file)
    file.save(basedir + '/' + dicts['images_path'])
    data = service.add(dicts)
    return data


@event_blue.route('/event/<ID>', methods=['delete'])
def deleteOld(ID):
    data = service.deleteByID(ID)
    return data

@event_blue.route('/img', methods=['get'])
def responseImg():
    imgDir = request.args.get('dir')
    return send_file(imgDir)

